NProgress.start();
window.onload = function () {
    NProgress.done();
}

layui.use(['element', 'table', 'form', 'jquery'], function () {
    var element = layui.element;
    var form = layui.form;
    var table = layui.table;
    var $ = layui.jquery;
    layer.load(2);
    table.render({
        elem: '#tableId',
        url: '/system/user/list',
        id: 'roleTable',
        page: false,
        cols: [[
            {type: 'numbers'},
            {field: 'username', minWidth: 100, title: '用户名'},
            {field: 'name', minWidth: 100, title: '姓名'},
            {field: 'mobile', minWidth: 100, title: '电话'},
            {field: 'status', minWidth: 85, title: '状态', templet: '#statusTpl'},
            {
                field: 'createTime',
                minWidth: 100,
                title: '添加时间',
                templet: function (d) {
                    return utils.dateFormat(d.createTime, "yyyy-MM-dd");
                }
            },
            {templet: '#operationTpl', width: 240, align: 'center', title: '操作'}
        ]]
        , response: {
            statusCode: true //重新规定成功的状态码为 200，table 组件默认为 0
        }
        , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
            return {
                "code": res.success, //解析接口状态
                "msg": res.data.errMsg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.records //解析数据列表
            };
        },
        done: function (res, curr, count) {
            layer.closeAll('loading');
            $("#countNum").text(count);
        }
    });
    layui.reload = function (data) {
        table.reload("roleTable", {where: data});
    };
    form.on('submit(search)', function (data) {
        layui.reload(data.field);
        return false;
    });

    table.on('tool(tableFilter)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            operater("编辑角色", '/system/user/edit/' + data.id);
        } else if (layEvent === 'del') {
            layer.confirm("确定要删除吗？", {skin: 'layui-layer-lan', icon: 2, title: '提示', anim: 6}, function () {
                $.post("/system/user/remove", {deptId: data.id}, function (res) {
                    if (res.success) {
                        layer.msg("操作成功！", {icon: 6, time: 700, anim: 4}, function () {
                            layui.reload({});
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                        });
                    } else {
                        parent.layer.alert(res.data.errMsg);
                    }
                }, 'json');
            });
        }
    });
    form.on('switch(status)', function (obj) {
        var userId = obj.value;
        var status = obj.elem.checked ? 1 : 0;
        $.post("/system/user/changeStatus", {userId: userId, status: status}, function (res) {
            if (res.success) {
                layer.msg("操作成功！", {icon: 6, time: 700, anim: 4});
            } else {
                parent.layer.alert(res.data.errMsg, function () {
                    layui.reload({});
                });
            }
        }, 'json');
    });
    var operater = function (title, url) {
        layer.open({
            title: title,
            type: 2,
            shade: false,
            maxmin: false,
            shade: 0.5,
            anim: 4,
            area: ['400px', '500px'],
            content: url,
            zIndex: layer.zIndex,
            end: function () {
                $(".layui-laypage-btn")[0].click();
            }
        });
    }
    $("#addUser").click(function () {
        operater("添加用户", "/system/user/add");
    })
})